﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using sdm.exam.domain;
using sdm.exam.domain.Abstract;
using sdm.exam.domain.DataContext;
using sdm.exam.domain.Entities;
using sdm.exam.domain.Manager;

namespace sdm.exam.test.Manager
{
    [TestClass]
    public class SemesterClassManagerTest
    {
        [TestMethod]
        public void GetListOfSemesterClassesTest()
        {
            Ninjector.Kernel.Bind<ISdmExamRepository>().ToConstant(new DataContext(ConnectionStrings.TestConnection));

            var manager = new sdm.exam.domain.Manager.SemesterClassManager();
            List<SemesterClass> semesterClassList = null;
            semesterClassList = manager.GetListOfSemesterClasses();

            Assert.IsNotNull(semesterClassList);
        }

        /// <summary>
        /// Checks whether or not the semester class i added.
        /// </summary>
        [TestMethod]
        public void AddSemesterClassTest()
        {
            Ninjector.Kernel.Bind<ISdmExamRepository>().ToConstant(new DataContext(ConnectionStrings.TestConnection));

            var semesterClassManager = new SemesterClassManager();
            const string name = "CS2010";
            var education = new Education() { Name = "Computer Science" };
            var startDate = new DateTime(2010, 9, 2);
            var endDate = new DateTime(2012, 2, 28);

            int listCountFirst = semesterClassManager.GetListOfSemesterClasses().Count;

            semesterClassManager.AddNewSemesterClass(name, startDate, endDate, education);

            int listCountSecond = semesterClassManager.GetListOfSemesterClasses().Count;

            Assert.AreNotEqual(listCountFirst, listCountSecond);
        }

        /// <summary>
        /// Edit a specific semester.
        /// </summary>
        [TestMethod]
        public void EditSemesterClassTest()
        {
            Ninjector.Kernel.Bind<ISdmExamRepository>().ToConstant(new DataContext(ConnectionStrings.TestConnection));
            var semesterClassManager = new domain.Manager.SemesterClassManager();

            const int id = 2;

            const string newName = "CS2009";
            string oldName = "";
            var newStartDate = new DateTime(2009, 9, 2);
            var newEndDate = new DateTime(2011, 2, 28);
            var newEducation = new Education() { Name = "Datamatiker" };

            var list = semesterClassManager.GetListOfSemesterClasses();

            SemesterClass semesterClass = list.FirstOrDefault(item => item.Id == id);

            if (semesterClass != null) oldName = semesterClass.Name;

            semesterClassManager.EddSemesterClass(id, newName, newStartDate, newEndDate, newEducation);

            if (semesterClass != null) Assert.AreNotEqual(oldName, semesterClass.Name);
        }

        /// <summary>
        /// Deletes a semester class.
        /// </summary>
        [TestMethod]
        public void DeleteSemesterClassTest()
        {
            Ninjector.Kernel.Bind<ISdmExamRepository>().ToConstant(new DataContext(ConnectionStrings.TestConnection));
            var semesterClassManager = new domain.Manager.SemesterClassManager();

            const int id = 2;

            int listFirst = semesterClassManager.GetListOfSemesterClasses().Count;

            semesterClassManager.DeleteSemesterClass(id);

            int listSecond = semesterClassManager.GetListOfSemesterClasses().Count;

            Assert.AreNotEqual(listFirst, listSecond);
        }
    }
}
